<?php
/**
 * Created by PhpStorm.
 * DateTime: 2020/11/4 15:22
 * Intro:
 */
namespace addons\podunk\library\traits;

use addons\podunk\library\CLog;
use addons\podunk\library\Redis;

trait GoldCache
{
    public function hasRedis($is_interrupt = false) {
        $error_msg = '';
        try {
            $redis = $this->getRedis();

            // 检测连接是否正常
            $redis->ping();
        } catch (\BadFunctionCallException $e) {
            // 缺少扩展
            $error_msg = $e->getMessage() ? $e->getMessage() : "缺少 redis 扩展";
        } catch (\RedisException $e) {
            // 连接拒绝
            \think\Log::write('redis connection redisException fail: ' . $e->getMessage());
            $error_msg = $e->getMessage() ? $e->getMessage() : "redis 连接失败";
        } catch (\Exception $e) {
            // 异常
            \think\Log::write('redis connection fail: ' . $e->getMessage());
            $error_msg = $e->getMessage() ? $e->getMessage() : "redis 连接异常";
        }

        if ($error_msg) {
            if ($is_interrupt) {
                throw new \Exception($error_msg);
            } else {
                return false;
            }
        }

        return true;
    }


    public function getRedis() {
        if (!isset($GLOBALS['PODREDIS'])) {
            $GLOBALS['PODREDIS'] = (new Redis())->getRedis();
        }

        return $GLOBALS['PODREDIS'];
    }

    public function setDeviceStatus($key)
    {
        $redis = $this->getRedis();

        if ($redis->EXISTS($key)) {
            $redis->EXPIRE($key, 1800);
        }else{
            $redis->lPush($key,'{"task_name":"","app":"","task":"","open_live_mode":"","open_live_text":"","product_name":"","start_time":9999999999999,"uuid":"00000000-0000-0000-0000-000000000000"}');
            $redis->EXPIRE($key, 1800);
        }
    }

    public function getDeviceStatus($key)
    {
        $redis = $this->getRedis();

        if($redis->EXISTS($key)){
            return true;
        }else{
            return false;
        }
    }

    public function getDeviceTaskList($key)
    {
        $redis = $this->getRedis();
        $this->setDeviceStatus($key);
        $listLength = $redis->lLen($key);
        if($listLength > 1){
            $list = $redis->lRange($key,0,$listLength - 2);
            foreach ($list as $k=>$v){
                $list[$k] = json_decode($v);
            }
            //对任务进行时间排序
            $times = array_column($list,'start_time');
            array_multisort($times,SORT_ASC,$list);
            return $list;
        }else
        {
            return null;
        }
    }

    public function getOnlineDevice($uid)
    {
        $redis = $this->getRedis();
        $deviceList = $redis->keys('*_'.$uid.'_*');
        return $deviceList;
    }

    public function pushTaskToKey($key, $data)
    {
        $redis = $this->getRedis();
        if($redis->EXISTS($key)){
            $redis->lPush($key, $data);
        }
    }

    public function getDeviceTaskNum($key)
    {
        $redis = $this->getRedis();
        return $redis->lLen($key);
    }

    public function getDeviceOnlineNum($uid)
    {
        $redis = $this->getRedis();
        $keys = $redis->keys('*_'.$uid.'_*');
        return count($keys);
    }

    public function removeTask($key, $value)
    {
        $redis = $this->getRedis();
        $list = $redis->lRange($key, 0, -1);
        //CLog::write($list);
        if($list){
            foreach ($list as $l){
                $x = json_decode($l);
                if($x->uuid == $value){
                    $redis->lRem($key, $l);
                }
            }
        }
        return true;
    }

    public function getInfo()
    {
        return $this->getRedis()->info();
    }

    public function test($key,$data)
    {
        $redis = $this->getRedis();

        $this->setDeviceStatus($key);
        $data = json_encode($data);
        $redis->lPush($key,$data);

    }
}